package cn.yfxu.server.controller;


import cn.yfxu.server.pojo.Admin;
import cn.yfxu.server.pojo.Result;
import cn.yfxu.server.pojo.Role;
import cn.yfxu.server.service.IAdminService;
import cn.yfxu.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yfxu
 * @since 2021-03-04
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    private IAdminService adminService;
    private IRoleService roleService;

    public AdminController(IAdminService adminService,IRoleService roleService) {
        this.adminService = adminService;
        this.roleService= roleService;
    }

    @ApiOperation(value = "获取所有操作员")
    @RequestMapping(method = RequestMethod.GET, value = "/")
    public List<Admin> getAllAdmins(String keywords) {
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value = "更新操作员")
    @RequestMapping(method = RequestMethod.PUT, value = "/")
    public Result updateAdmin(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return Result.success("更新成功！");
        }
        return Result.error("更新失败！");
    }
    @ApiOperation(value = "删除操作员")
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteAdmin(@PathVariable Integer id) {
        if (adminService.removeById(id)) {
            return Result.success("删除成功！");
        }
        return Result.error("删除失败！");
    }

    @ApiOperation(value = "获取所有角色")
    @RequestMapping(method = RequestMethod.GET,value = "/roles")
    public List<Role> getAllRoles(){
        return roleService.list();
    }
    @ApiOperation(value = "更新操作员角色")
    @RequestMapping(method = RequestMethod.PUT,value = "/roles")
    public Result updateAdminRole(Integer adminId,Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }
}
